package com.mcxx.redis.controller;

import com.mcxx.redis.client.RedisClusterUtils;
import com.mcxx.redis.constant.RedisGlobalConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


@Slf4j
@Api("Redis管理工具")
@RestController("redis/test")
public class RedisController {
    @Autowired
    private RedisClusterUtils redisClusterUtils;

    @PostMapping("testRedis")
    @ApiOperation("Redis缓存测试")
    public void testRedis(){
        redisClusterUtils.set("test12345", new Date(), 300);
        System.out.println(redisClusterUtils.hasKey("test12345"));
        System.out.println(redisClusterUtils.get("test12345"));

        Map<String, Object> map = new HashMap<>();
        map.put("name", "hello");
        redisClusterUtils.hmset("test", map);

        for(int i=0; i<20; ++i) {
            redisClusterUtils.set("mykey" + i, map, 300);
        }

        redisClusterUtils.convertAndSend("redisTestListener", map);
    }

    @PostMapping("testGetRedisMap")
    @ApiOperation("Redis读取Map缓存")
    public void testGetRedisMap(){
        Map<String, Object> map = redisClusterUtils.hmget("test");
        log.info("map:{}", map);

        Set<String> keys = redisClusterUtils.hasKeys("mykey");
        Iterator it = keys.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }

        long count = redisClusterUtils.del("mykey10", "mykey1");
        System.out.println(count);
    }
}
